package com.dhecp.project.logstics.controller;


import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.DateUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.logstics.domain.FbaDevanning;
import com.dhecp.project.logstics.domain.FbaDevanningDetail;
import com.dhecp.project.logstics.mapper.FbaDevanningDetailMapper;
import com.dhecp.project.logstics.service.FbaDevanningService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: kjecp
 * @description: 头程拆箱单 控制层
 * @author: hlf
 * @create: 2022-08-15 15:13
 */
@Controller
@RequestMapping("/logstics/fbaDevanning")
public class FbaDevanningController extends BaseModuleController {

    private String prefix = "logstics/fbaDevanning";
    private String moduleTable = ModuleTable.FBA_DEVANNING;
    private String moduleTable2 = ModuleTable.FBA_DEVANNING_DETAIL;

    @Autowired
    private FbaDevanningService fbaDevanningService;

    @Autowired
    private FbaDevanningDetailMapper fbaDevanningDetailMapper;


    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: hlf
     * @date: 2022-08-15 15:43
     */
    @RequiresPermissions("business:fbaplanorder:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        if (params.get("num").equals("0")){
            params.put("state", "0");
        } else {
            params.put("state", "1");
        }
        List<FbaDevanning> list = new ArrayList<>();
        try {
            list = fbaDevanningService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
     * @Description: 打开制单页
     * @Param: nid 必要参数：单据编码（新增时传默认值 0 ，修改、查看时传单据实际值）
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-08-15 15:42
     */
    @RequiresPermissions("business:fbaplanorder:view")
    @GetMapping("/edit")
    public ModelAndView editPage(@RequestParam Map<String, Object> params) {
        if (params.get("nid") == null || params.get("nid") == "") {
            params.put("nid", "0");
        }
        params.put("moduleTable", moduleTable);
        params.put("entity", new FbaDevanning()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        params.put("moduleUrl", prefix); //页面上级路径
        params.put("prefix", prefix + "/fbaDevanning_edit"); //页面路径
        Subject userSubject = ShiroUtils.getSubject();
        int enableEdit = 0;
        boolean vType = false;
        if (params.get(enableEdit) == null || "1".equals(params.get(enableEdit).toString())) {
            if (userSubject.isPermitted("business:fbaplanorder:edit")) { //当前登录用户主体是否有修改的权限
                enableEdit = 1;
            }
        }

        try {
            Map<String, Object> headData = new HashMap<String, Object>();        //表头的业务数据
            FbaDevanning businessData = fbaDevanningService.queryFirstRecord(params);
            if (businessData != null) {
                headData = TypeUtils.entityToMap(businessData);
                if (businessData.getState().equals("1")){
                    vType = true;
                }
            }
            params.put("headData", headData); //表头的业务数据

            params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
            ModelAndView mv = this.openEditPage(params);
            mv.addObject("nowTimeStr", DateUtils.getDate());    //当前日期
            mv.addObject("enableEdit", vType ? 0 : enableEdit); //当前页面是否可编辑（0：不可编辑，1：可编辑）
            if (businessData != null) {
                Map<String, Object> params3 = new HashMap<String, Object>();
                params3.put("code",businessData.getCodeDevanning());
                params3.put("moduleTable", moduleTable2);
                mv.addObject("List", fbaDevanningDetailMapper.queryList(params3));//子件数据
            }
            params.put("moduleTable", moduleTable2);
            params.put("entity", new FbaDevanningDetail());
            params.put("readOnly", vType);//生成列是否可编辑
            params.put("toolBarWidth", 0);//操作区
            params.put("checkBox", "yes");
            params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段
            mv.addObject("gridColumnsZJ", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));

            return mv;
        } catch (Exception e) {
            return this.gotoModelAndViewErrorPage(e.getMessage());    //详细信息
        }

    }

    /**
     * @Description: 数据导出Excel
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-15 15:43
     */
    @RequiresPermissions("business:fbaplanorder:export")
    @Log(title = "头程拆箱数据导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);
        params.put("entity", new FbaDevanning());

        List<FbaDevanning> list = fbaDevanningService.queryList(params);

        params.put("getFieldAttributesType", "export");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<FbaDevanning> util = new ExportExcelUtils<FbaDevanning>(moduleTable, new FbaDevanning());
        String sheeName = "";
        if (params.get("num").equals("4")){
            sheeName = "头程拆箱单";
        } else {
            sheeName = "头程拆箱完成单";
        }
        return util.exportExcel(list, fieldList, sheeName, "export");
    }

    /**
     * @Description: 保存数据
     * @Param: infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-15 15:44
     */
    @RequiresPermissions(value = {"business:fbaplanorder:add", "business:fbaplanorder:edit", "business:fbaplanorder:delete"}, logical = Logical.OR)
    @Log(title = "头程拆箱单", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editpost(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = fbaDevanningService.saveData(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * @Description: 修改审核状态
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-17 14:55
     */
    @RequiresPermissions("business:fbaplanorder:list")
    @Log(title = "修改头程审核状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateState")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateState(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = fbaDevanningService.updateState(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}